package com.example.musicplayer.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.example.musicplayer.R;
import com.example.musicplayer.entity.MusicListTest;

import java.util.List;

public class MusicListTestAdapter extends RecyclerView.Adapter<MusicListTestAdapter.ViewHolder> {

    private List<MusicListTest> mMusicListTests;
    @LayoutRes
    private int resource;


    /**
     * 适配器构造函数
     *
     * @param musicListTests 传入音乐列表集合
     * @param resource       传入RecyclerView的item的样式
     */
    public MusicListTestAdapter(List<MusicListTest> musicListTests, @LayoutRes int resource) {
        this.mMusicListTests = musicListTests;
        this.resource = resource;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView musicListImg;
        TextView musicListTitle;
        View musicListView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            musicListImg = itemView.findViewById(R.id.music_list_img);
            musicListTitle = itemView.findViewById(R.id.music_list_title);
            musicListView = itemView;
        }

    }

    @NonNull
    @Override
    public MusicListTestAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(parent.getContext()).inflate(resource, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MusicListTestAdapter.ViewHolder holder, int position) {
        MusicListTest musicListTest = mMusicListTests.get(position);
        holder.musicListImg.setImageResource(musicListTest.getImageId());
        holder.musicListTitle.setText(musicListTest.getTitle());
    }

    @Override
    public int getItemCount() {
        return mMusicListTests.size();
    }
}
